package com.anbinh.doctor.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.anbinh.doctor.DetailtActivity;
import com.anbinh.doctor.R;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.DoctorInfo;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.IRcallBak;
import com.anbinh.doctor.utils.Utils;

public class DoctorInfoAdapter extends BaseAdapter {

	private static final int TYPE_ITEM = 0;
	public static final int TYPE_SEPARATOR = 1;
	private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

	private LayoutInflater mInflater;
	private Context context;
	private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
	private List<DoctorInfo> mData = new ArrayList<DoctorInfo>();
	private UserLogin userLogin;
	private IRcallBak callBak;

	public DoctorInfoAdapter(Context mContext, IRcallBak caIRcallBak) {
		mInflater = LayoutInflater.from(mContext);
		context = mContext;
		userLogin = Utils.getAccountModel(mContext, Utils.KEY_USER);
		this.callBak = caIRcallBak;
	}

	public void addItem(final DoctorInfo item) {
		mData.add(item);
		notifyDataSetChanged();
	}

	public void removeItem(int position) {
		mData.remove(position);
		notifyDataSetChanged();
	}

	public void RemoveData() {
		mData.removeAll(mData);
		mSeparatorsSet.removeAll(mSeparatorsSet);
		notifyDataSetChanged();
	}

	public void addSeparatorItem(final DoctorInfo item) {
		mData.add(item);
		mSeparatorsSet.add(mData.size() - 1);
		notifyDataSetChanged();
	}

	@Override
	public int getItemViewType(int position) {
		return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
	}

	@Override
	public int getViewTypeCount() {
		return TYPE_MAX_COUNT;
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public DoctorInfo getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;

		int type = getItemViewType(position);
		if (convertView == null) {
			holder = new ViewHolder();
			switch (type) {
			case TYPE_SEPARATOR:

				convertView = mInflater.inflate(R.layout.item_seprator_doctor,
						null);
				holder.lblTitle_show = (TextView) convertView
						.findViewById(R.id.lblTitle_show);
				holder.lblTitle_add = (TextView) convertView
						.findViewById(R.id.lblTitle_add);

				break;

			case TYPE_ITEM:

				convertView = mInflater.inflate(R.layout.item_sub_thongtinthem,
						null);
				holder.lblChuyenKhoaName = (TextView) convertView
						.findViewById(R.id.lblChuyenKhoaName);
				holder.lblDesc = (TextView) convertView
						.findViewById(R.id.lblDesc);
				holder.btnSetup = (LinearLayout) convertView
						.findViewById(R.id.btnSetup);

				break;
			}
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final DoctorInfo item = getItem(position);
		if (type == TYPE_SEPARATOR) {
			holder.lblTitle_show.setText(item.getTitle().toUpperCase());
			holder.lblTitle_add.setText("Thêm " + item.getTitle());

		} else {
			Log.d("name", item.getTitle());
			holder.lblChuyenKhoaName.setText(item.getTitle());
			if (item.getDescription().isEmpty()) {
				holder.lblDesc.setVisibility(View.GONE);
			} else {
				holder.lblDesc.setText(item.getDescription());
			}
			int id = item.getSeparatorId();
			if (id == R.id.chuyen_khoa || id == R.id.chuc_danh
					|| id == R.id.ngon_ngu) {
				holder.btnSetup.setVisibility(View.GONE);
			} else {
				holder.btnSetup.setVisibility(View.VISIBLE);
			}

			holder.btnSetup.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if ((DetailtActivity) context != null)
						showPopupMenu(item, position);

				}

			});

		}

		return convertView;
	}

	public void showPopupMenu(final DoctorInfo item, final int location) {
		final Dialog dialog = new Dialog(context);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setBackgroundDrawableResource(
				R.drawable.abs__menu_dropdown_panel_holo_light);
		dialog.setCanceledOnTouchOutside(true);
		DisplayMetrics metrics = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);

		int width = metrics.widthPixels;

		dialog.setContentView(R.layout.dialog_menu_extaned);
		dialog.getWindow().setLayout(width - 60, LayoutParams.WRAP_CONTENT);

		TextView lblXoaDc = (TextView) dialog.findViewById(R.id.lblXoaDc);
		lblXoaDc.setText("Xóa " + item.getGroupName());

		lblXoaDc.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onDelete(item);
				dialog.dismiss();
			}
		});
		try {
			dialog.show();
		} catch (Exception e) {

		}
	}

	public void onDelete(final DoctorInfo item) {
		ServerController.getInstance(context).requestRemoveExtanedData(
				userLogin.getData().getToken(), item.getRegId(),
				item.getSeparatorId(), new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						if (data.getResponsecode() >= 0) {
							callBak.onDisplay(Activity.RESULT_OK, null);
						}
						Utils.showMessage(((Activity) context),
								data.getDescription());
					}

					@Override
					public void onError(int errorCode, String message) {

					}

				});
	}

	public static class ViewHolder {
		public TextView lblTitle_show;
		public TextView lblTitle_add;

		public ImageView thumnail;
		public TextView lblChuyenKhoaName;
		public TextView lblDesc;
		public TextView lblThoigian;
		public LinearLayout btnSetup;
		public LinearLayout lineMore;
	}

}